package com.example.texted.completablefuture;

import java.util.concurrent.CompletableFuture;

public class Do2 {
    public static void main(String[] args) throws Exception {
        // 第一个任务:
        CompletableFuture<String> cfQuery = CompletableFuture.supplyAsync(Do2::queryCode);
        // cfQuery成功后继续执行下一个任务:
        CompletableFuture<Integer> cfFetch = cfQuery.thenApplyAsync(Do2::fetchPrice);
        // cfFetch成功后打印结果:
        cfFetch.thenAccept((result) -> {
            System.out.println("price: " + result);
        });
        // 主线程不要立刻结束，否则CompletableFuture默认使用的线程池会立刻关闭:
        Thread.sleep(2000);
    }

    static String queryCode() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException ignored) {
        }
        return "601857";
    }

    static Integer fetchPrice(String code) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException ignored) {
        }
        return Integer.parseInt(code)+3;
    }
}
